package com.example.administrator.szb.util;

import android.app.Activity;
import android.text.TextUtils;

import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.config.TagAliasOperatorHelper;
import com.example.administrator.szb.http.HttpUtils;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

import static com.example.administrator.szb.config.TagAliasOperatorHelper.ACTION_DELETE;
import static com.example.administrator.szb.config.TagAliasOperatorHelper.ACTION_SET;
import static com.example.administrator.szb.config.TagAliasOperatorHelper.sequence;

/**
 * Created by stone on 2018/1/16.
 */

public class JPushUtils {
    public static void delete(){
            String phone = SPUtils.getDatasString(SPUtils.USER_PHONE);

        if (!TextUtils.isEmpty(phone))
        {
            TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
            tagAliasBean.action = ACTION_DELETE;
            sequence++;
            tagAliasBean.alias = phone;
            tagAliasBean.isAliasAction = true;
            TagAliasOperatorHelper.getInstance().handleAction(SampleApplicationLike.getInstance(),sequence,tagAliasBean);
        }
    }
    public static void setALias(Activity context, String phone){
        HttpUtils.requestBD(context);
        if (phone==null){
            phone = SPUtils.getDatasString(SPUtils.USER_PHONE);
        }
        if (!TextUtils.isEmpty(phone)&&!SPUtils.jpushSuccess())
        {
            TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
            tagAliasBean.action = ACTION_SET;
            sequence++;
            tagAliasBean.alias = phone;
            tagAliasBean.isAliasAction = true;
            TagAliasOperatorHelper.getInstance().handleAction(SampleApplicationLike.getInstance(),sequence,tagAliasBean);
        }
    }
    public static void register(final String userName,final String password){
        //详细代码如下
        JMessageClient.register(userName,password, new BasicCallback() {
            @Override
            public void gotResult(int code, String s) {
                QTLog.e(code+"注册"+s);
                if(code==0||code==898001){
                    QTLog.e("注册成功，可进行相应的逻辑操作");
                    login(userName,password);

                }
            }
        });
    }
    public static void login(String userName,String password){
        //详细代码如下
        JMessageClient.login(userName,password, new BasicCallback() {
            @Override
            public void gotResult(int code, String s) {
                QTLog.e(code+"登录"+s);
                if(code==0){
                    //登录成功，可进行相应的逻辑操作
                    QTLog.e("登录成功，可进行相应的逻辑操作");

                }
            }
        });
    }

    public static void updateUserPassword(String userName,String password){
        //详细代码如下
        JMessageClient.updateUserPassword(userName,password, new BasicCallback() {
            @Override
            public void gotResult(int code, String s) {
                if(code==0){

                }
            }
        });
    }
    public static void logout(){
        JMessageClient.logout();//极光退出登录

    }


}
